package com.quyou.funtour.utils;

import com.alibaba.fastjson.JSON;
import com.quyou.funtour.bean.WechatRespoonse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Component
public class WechatUtils {

    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;
    @Value("${wechat.grant_type}")
    private String grant_type;

    @Autowired
    private  RestTemplate restTemplate;


    /**
     * 调用微信的接口获取用户登录信息
     * @param code
     * @return
     */
    public WechatRespoonse login(String code){
        String forObject = restTemplate.getForObject("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=" + grant_type,
                String.class);
        WechatRespoonse wechatRespoonse = JSON.parseObject(forObject, WechatRespoonse.class);
        return wechatRespoonse;
    }

}
